import sys

x, y = map(int, input().split())
place = [[] for i in range(x)]

book = {}
bs = []
now = 1
for i in range(x):
    place[i] = list(map(int, sys.stdin.readline().strip('\n').split()))
for i in range(y):
    c, z = map(int, input().split())
    if c == 0:
        now = place[now - 1][z]
    elif c == 1:
        book[z] = now
        bs.append(now)
    else:
        now = book[z]
for i in bs:
    print(i)
print(now)
